import {ChangeDetectorRef, Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {ClientPolicyListProvider} from "../../../providers/client/client-policy-list/client-policy-list";
import * as moment from 'moment';
import {LoadingService} from "../../../services/common/loading.service";

@IonicPage()
@Component({
    selector: 'page-client-policy-list',
    templateUrl: 'client-policy-list.html',
    providers: [
        ClientPolicyListProvider
    ]
})
export class ClientPolicyListPage {

    moment = moment;

    clientNum: string;
    policyList: any;
    isScrolling = false;
    isArriveBottom = false;
    isLoaded: boolean = false;
    @ViewChild(Content) content: Content;

    constructor(public navCtrl: NavController,
                public clientPolicyListProvider: ClientPolicyListProvider,
                public changeRef:ChangeDetectorRef,
                private loadingService: LoadingService,
                public navParams: NavParams,
                public toast: ToastController,
                ) {
    }

    ionViewDidEnter(){
        // this.clientNum = this.navParams.get('clientNum');
        // // console.log(this.clientNum);
        // this.loadingService.showLoading();
        // this.clientPolicyListProvider.getClientPolicyList(this.clientNum).subscribe(res => {
        //     this.policyList = res;
        //     this.isLoaded = true;
        //     // console.log(this.policyList);
        //     this.loadingService.dismissLoading();
        // }, error2 => {
        //     this.loadingService.dismissLoading();
        // });
    }

    ionViewDidLoad() {
        this.clientNum = this.navParams.get('clientNum');
        // console.log(this.clientNum);
        this.loadingService.showLoading();
        this.clientPolicyListProvider.getClientPolicyList(this.clientNum).subscribe(res => {
            this.policyList = res;
            this.isLoaded = true;
            // console.log(this.policyList);
            this.loadingService.dismissLoading();
        }, error2 => {
            this.loadingService.dismissLoading();
        });
    }

    backButtonClick(ev) {
        ev.preventDefault();
        ev.stopPropagation();
        this.navCtrl && this.navCtrl.pop(null, null);
    }

    // goToDetail(policyNO: string, orphanType: string, agentCode: string) {
    //     this.navCtrl.push('PolicyDetailPage', {
    //         policyNO: policyNO,
    //         orphanType: orphanType,
    //         agentCode: agentCode?agentCode:''
    //     });
    // }

    goToDetail(policyNO: string,
               policyPayToDate: string,
               policyStatus: string,
               policyStatusCode: string,
               orphanType: string,
               agentCode: string) {
        this.navCtrl.push('PolicyDetailsPage', {
            policyNO: policyNO,
            policyPayToDate: policyPayToDate,
            policyStatus: policyStatus,
            policyStatusCode: policyStatusCode,
            orphanType: orphanType,
            agentCode: agentCode?agentCode:''
        });
    }

    getSex(sex: string): string {
        if (sex === 'M') return 'nan';
        if (sex === 'F') return 'nv';
        if (sex === '') return 'unknow';
    }
    /**
     *  toast 提示信息
     * @param {string} message 提示内容
     */
    toastCtrl(message: string) {
        this.toast.create({
            message: message,
            position: 'middle',
            duration: 1200
        }).present();
    }

    /**
     * 跳转到投保人详情
     */
    goToHolderDetail(id){
        this.navCtrl.push("HolderClientDetailPage",{"clientNum":id})
    }


    /**
     * 跳转到被保人详情
     */
    goToInsuredDetail(id){
        this.navCtrl.push("HolderClientDetailPage",{"clientNum":id})
    }

    pullDownRefresh(refresher) {
        this.clientPolicyListProvider.getClientPolicyList(this.clientNum).subscribe(res => {
            this.policyList = res;
            refresher.complete();
        }, error2 => {
            refresher.complete();
        });
    }

    scrollStart(e){
        this.isScrolling = true;
    }
    scrollEnd(e){
        this.isScrolling = false;
    }

    scrolling(){
        const dimensions = this.content.getContentDimensions();
        const temp1 = dimensions.scrollHeight - dimensions.scrollTop - dimensions.contentHeight < 100;

        if(temp1 !== this.isArriveBottom){
            this.isArriveBottom = temp1;
            this.changeRef.detectChanges();
        }
    }

    goUp() {
        if(this.isScrolling){
            setTimeout(() => {
                this.goUp();
            },300);
            return;
        }
        this.content.scrollToTop();
    }
}
